package com.scan.bus.util;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.app.ActivityManager;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.location.Location;
import android.location.LocationManager;
import android.net.Uri;
import android.os.Build;
import android.text.SpannableStringBuilder;
import android.text.TextUtils;
import android.view.View;
import android.view.inputmethod.InputMethodManager;
import android.widget.Toast;

import com.scan.bus.MyApp;

import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;


/**
 * Created by zhang on 2017/4/14 11 : 42.
 */

public class Common {
    private static Toast toast;
    private static SpannableStringBuilder ssb;
    private static List<String> currentWordList;
    public static final String no_clear_wx_jump = "no_clear_wx_jump";

    public static boolean isActDestroy(Context context) {
        if (context == null || (context instanceof Activity && Common.isActDestroy((Activity) context))) {
            return true;
        } else {
            return false;
        }
    }

    public static boolean isActDestroy(Activity mActivity) {
        if (mActivity == null || mActivity.isFinishing() || (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1 && mActivity.isDestroyed())) {
            return true;
        } else {
            return false;
        }
    }

    public static boolean isColor(String value) {
        if (TextUtils.isEmpty(value))
            return false;
        String type = "^#[0-9a-fA-F]{6}$";
        Pattern pattern = Pattern.compile(type);
        Matcher matcher = pattern.matcher(value);
        return matcher.matches();
    }

    /**
     * @return true栈顶栈底都是当前act，false不唯一
     */

    public static boolean isUniqueAct(String className) {
        try {
            ActivityManager manager = (ActivityManager) MyApp.getContext().getSystemService(Context.ACTIVITY_SERVICE);
            String baseName = manager.getRunningTasks(1).get(0).baseActivity.getClassName();
            String topName = manager.getRunningTasks(1).get(0).topActivity.getClassName();
            return (baseName.equals(className) && topName.equals(className));
        } catch (Exception e) {
            return true;
        }
    }

    @SuppressLint("MissingPermission")
    public static Location getLocation(Context context){
        if (context == null)return null;
        LocationManager locationManager = (LocationManager) context.getSystemService(Context.LOCATION_SERVICE);
        List<String> providers = locationManager.getProviders(true);
        for(String provider : providers){
            Location location = locationManager.getLastKnownLocation(provider);
            if (location != null){
                return location;
            }
        }
        return null;
    }

    //获取经纬度
//    @SuppressLint("MissingPermission")
//    public static Location getGPS(final Activity activity, boolean isHint) {
//        try {
//            // 获取位置管理服务
//            LocationManager locationManager;
//            String serviceName = Context.LOCATION_SERVICE;
//            locationManager = (LocationManager) activity.getSystemService(serviceName);
//
//            // 查找到服务信息
//            Criteria criteria = new Criteria();
//            criteria.setAccuracy(Criteria.ACCURACY_FINE); // 高精度
//            criteria.setAltitudeRequired(false);
//            criteria.setBearingRequired(false);
//            criteria.setCostAllowed(true);
//            criteria.setPowerRequirement(Criteria.POWER_LOW); // 低功耗
//
////        String provider = locationManager.getBestProvider(criteria, true); // 获取GPS信息
//
//            Location location = null;
//            if (locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER)) {
//                location = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER); // 通过GPS获取位置
//                //locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, this);
//
//                if (location == null) {
//                    if (locationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER)) {
//                        location = locationManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER); // 通过NETWORK获取位置
//                        //locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0, this);
//                    }
//                }
//                if (location == null && isHint)
//                    locateDialog(activity);
//                return location;
//            } else if (locationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER)) {
//                location = locationManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER); // 通过NETWORK获取位置
//                //locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0, this);
//                if (location == null && isHint)
//                    locateDialog(activity);
//                return location;
//            } else {
//                if (isHint)
//                    locateDialog(activity);
//                return null;
//            }
//        } catch (Exception e) {
//            return null;
//        }
//    }



    private static Toast exToast;

    /**
     * 获取全局上下文
     *
     * @return
     */
    public static Context getApplicationContext() {
        return MyApp.mApp;
    }

    /**
     * 部分手机以为上下文的原因显示不出提示，建议使用两个参数的方法
     *
     * @param content 显示内容
     * @see #staticToast(Context, String)
     */
    public static void staticToast(String content) {
        staticToast(MyApp.getContext(), content);
    }

    public static void staticToast(Context context, String content) {
        try {
            if (TextUtils.isEmpty(content) || isActDestroy(context))
                return;
            Toast.makeText(context ,content, Toast.LENGTH_LONG).show();
        } catch (Exception e) {
            e.getMessage();
        }
    }


    //隐藏虚拟键盘
    public static void hideKeyboard(View view) {
        InputMethodManager imm = (InputMethodManager) view.getContext().getSystemService(Context.INPUT_METHOD_SERVICE);
        if (imm.isActive()) {
            imm.hideSoftInputFromWindow(view.getApplicationWindowToken(), 0);
        }
    }

    public static boolean HasAliPay(Context context) {
        Uri uri = Uri.parse("alipays://platformapi/startApp");
        Intent intent = new Intent(Intent.ACTION_VIEW, uri);
        ComponentName componentName = intent.resolveActivity(context.getPackageManager());
        return componentName != null;
    }

    //显示虚拟键盘
    public static void showKeyboard(View v) {
        v.setFocusable(true);
        v.setFocusableInTouchMode(true);
        v.requestFocus();
        InputMethodManager imm = (InputMethodManager) v.getContext().getSystemService(Context.INPUT_METHOD_SERVICE);
        imm.showSoftInput(v, InputMethodManager.SHOW_FORCED);
    }

    /**
     * 获取占位符 一个占位符代表一个3个字节
     *
     * @param num
     * @return
     */
    @Deprecated
    public static String getPlaceholder(int num) {
        String str = "\u3000";
        if (num == 1) str += " ";
        for (int i = 0; i < num; i++) {
            str = str.concat("\u3000");
        }
        return str;
    }

}
